package update

import (
	"net/http"
	"strings"
)

func UpgradeService(addr string, handler *http.ServeMux) error {
	return http.ListenAndServe(addr, handler)
}
func UpgradeServiceTLS(addr string, handler *http.ServeMux, certFile, keyFile string) error {
	return http.ListenAndServeTLS(addr,
		certFile,
		keyFile,
		handler)
}

//http接口重定向到转https
func HttpToHttps(httpAddr, httpsAddr string) {
	go http.ListenAndServe(":"+httpAddr, http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
		host := strings.Split(req.Host, ":")
		host[1] = httpsAddr
		target := "https://" + strings.Join(host, ":") + req.URL.Path
		if len(req.URL.RawQuery) > 0 {
			target += "?" + req.URL.RawQuery
		}
		http.Redirect(w, req, target, http.StatusTemporaryRedirect)
	}))

}
